Thursday, 30 October 2014

Capturing Scala profile data from the command line

Sometimes it's just not practical to attach a profiler to a running application. For instance you may want to profile your application's startup code.

If you're using Oracle's JDK you can solve this problem with command line parameters which start Java Flight Recorder as your application starts up. The available parameters are somewhat byzantine so it's worth your time to read the command line documentation

As an example, I use the VM parameters below when I want to profile an integration test from inside IntelliJ:

-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=defaultrecording=true -XX:FlightRecorderOptions=dumponexit=true,dumponexitpath=/Some/Folder

If you use these parameters a .jfr file containing profile data will be written to /Some/Folder when your application exits. You can then open the file with Java Mission Control.

If .jfr file associations aren't set up you can launch Java Mission Control on a Mac from /usr/bin/jmc.

This is what it looks like - not bad for a tool which is free to use!

Tools for profiling your Scala app

One of the neat things about Scala (and other JVM languages) is that the JDK comes bundled with free profilers.

Both Oracle's JDK and OpenJDK are bundled with the open source Java VisualVM profiler.

Oracle's JDK also comes bundled with Java Mission Control which is free to use on development machines (even though it is a commercial product).

To my eye Java Mission Control seems a better choice in a lot of cases. Having said that each tools has different capabilities.

For details on when to use each profiler, what other tools are available if neither profiler solves your problem, and the commercial license for Java Mission Control take a look at Oracle's Diagnostic Tools and Detailed Descriptions page.

Wednesday, 3 September 2014

Using Scala's FiniteDuration DSL

As a relatively new Scala developer I often find myself hunting for the the correct imports to enable various implicit conversions. A particularly useful set of implicit conversions are those that convert an Int / Long / Double to a FiniteDuration. They allow you to write code like:
val timeout1 = 3 seconds
val timeout2 = 3.5 minutes
To enable this syntax you need to import one or more of the following:
import scala.concurrent.duration.{DurationInt, DurationLong, DurationDouble}

Tuesday, 20 March 2012

Debugging UIWebView content in a MonoTouch app

Desktop browsers have great support for diagnosing rendering issues or scripting bugs.

But what are you supposed to do if you're developing an iPhone or iPad app, and you need to debug web content hosted inside a UIWebView control?

It turns out that you can use desktop Safari to "remotely" inspect mobile Safari when its running in the iOS Simulator. Happy days!

To pull this off your app has to call a private API, or at least it does in iOS 5.1 - hopefully Apple will expose this API "properly" at some point.

If you want to see how to do this in Objective-C head over to Nathan de Vries' post. If you prefer writing iOS apps in C# here's what to do:

First add the following method to your AppDelegate class:

[Conditional("DEBUG")]
private static void EnableUIWebViewRemoteInspector()
{
    var webViewClass = Class.GetHandle("WebView");
    var selector = Selector.GetHandle("_enableRemoteInspector");

    Messaging.void_objc_msgSend(webViewClass, selector);
}

You'll notice that EnableUIWebViewRemoteInspector() is conditionally compiled into DEBUG builds only. Your app will most likely be rejected by Apple if you submit it with calls to private APIs, so make sure you submit a RELEASE build and/or remove EnableUIWebViewRemoteInspector() once you're done debugging.

Next call the new method from your AppDelegate's FinishedLaunching method:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{   
    EnableUIWebViewRemoteInspector();   
    return true;
}

That's it!

To try it out: fire up your app in the iOS Simulator and browse to http://localhost:9999 using desktop Safari (the remote inspector doesn't work properly in Chrome or Firefox).

You'll be presented with a list of pages you can inspect. When you pick one this is what you should see:

Wednesday, 1 February 2012

LLVM syntax highlighting in Sublime Text 2

If you use a Mac for any kind of development work you should give Sublime Text 2 a try.

It's a seriously awesome editor:
  • It's pretty.
  • It's fast.
  • It supports a good selection of languages.
  • It's got nifty features such as "goto anything" and "auto completion" that you usually only find in full featured IDEs. 
  • As an added bonus it's Australian made. 

Go and download it now!

Seriously :)

One of the languages that Sublime Text 2 doesn't support out of the box is LLVM's assembly language.  (i.e. syntax highlighting doesn't work for .ll files).


Adding LLVM support is really easy because Sublime Text 2 is more or less compatible with TextMate syntax definition files, and an LLVM TextMate bundle is already available on github.

The Sublime Text 2 site has good documentation on how to add new packages. The short story is that you can add LLVM support by doing the following:
  1. Start Sublime Text 2.
  2. Use the "Sublime Text 2/Preferences/Browse Packages" menu to open the packages folder in Finder.
  3. Create a new folder called LLVM (the name isn't really important - use another name if you prefer).
  4. Save llvm.tmbundle from github into your new folder.
Sublime Text 2 will start using the new package without a restart, so go ahead and open an .ll file or two :)

Friday, 11 November 2011

Not yet a post PC world

Apple's last major update to IOS (IOS 5) included an "over the air update" feature. The idea being that you should be able to upgrade the software on your iPad etc even if you have never purchased a Mac or a PC. Apple wants us to think that we've entered a "post PC era".

Now that IOS 5.0.1 has been released we get to try over the air updates for the first time.

When I tried to update my iPad I got the following message (after a lengthy process): "An error occurred installing IOS 5.0.1". Argh!


This error is not particularly helpful, and using only your iPad there doesn't seem to be any way to get further details about what went wrong, or to retry the update. Basically all you get is "iPad says no". So much for the post PC era :(

Anyway my next attempt was to tether to my Mac via USB and try updating via iTunes. This also failed (after another lengthy process) with: "The iPad ... could not be updated. An unknown error occurred (1654)". Still annoying, but at least there's an error number to help track down a solution.


The solution was to put my iPad into recovery mode and do a full restore (as per http://support.apple.com/kb/ht4097).

In hindsight I could probably have done the restore from my iCloud backup rather than the backup on my Mac. Maybe the post PC era isn't as far away as I thought?

Regardless, this kind of experience might be fine for us geeks, but does Apple seriously expect my Mum to be able to do this?

The on-device trouble shooting experience really needs to be a lot more robust. Ideally error messages would contain some details on what actually went wrong, along with something like "click here for suggestions on how to resolve this issue yourself, or here to make an appointment with the Genius Bar so we can fix the problem for you".

To be fair the update worked on my iPhone flawlessly.